package car.aistar.service.impl;

import car.aistar.dto.XrbRecent;
import car.aistar.entity.CarDetail;
import car.aistar.entity.CarRecord;
import car.aistar.service.*;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @ClassName DealCarServiceImpl
 * @Description TODO 成交车辆条件搜索(品牌名，车系名，时间 年月) 查询成交
 * @Author laoxu
 * @Date 2020/10/3 19:32
 * Version 1.0
 **/

@Service("dealCarService")
public class DealCarServiceImpl implements DealCarService {

    @Autowired
    private CarRecordService carRecordService;

    @Autowired
    private CarBrandsService carBrandsService;

    @Autowired
    private CarStyleService carStyleService;

    @Autowired
    private CarDetailService carDetailService;


    @Override
    public List<XrbRecent> queryDealCar(String brName, String stName,String year,String month) {
        String date1 = year+"-"+month+"-01 00:00:00";
        Date date = DateUtil.parse(date1);
        Date beginOfDay = DateUtil.beginOfMonth(date);
        Date endOfDay = DateUtil.endOfMonth(date);
        //查询月第一天
        java.sql.Date transDate = new java.sql.Date(beginOfDay.getTime());
        //查询月最后一天
        java.sql.Date transDate2 = new java.sql.Date(endOfDay.getTime());
        //条件查出对应成交车辆
        List<CarRecord> carRecords = carRecordService.findCarRecord(brName,stName,transDate,transDate2);
        List<XrbRecent> xrbRecentList = new ArrayList<>();
        for (CarRecord c : carRecords) {
            XrbRecent xrbRecent = new XrbRecent();
            Long cid = c.getcId();//成交车id

            Date time = c.getReTime();//成交时间

            xrbRecent.setId(cid);
            xrbRecent.setRePrice(c.getRePrice());//成交价格
            CarDetail carDetail = carDetailService.queryById(cid);
            //品牌id
            Long brId = carDetail.getBrId();
            //车系id
            Long stId = carDetail.getStId();

            Date time2 = carDetail.getCCreatedate();//上架时间
            //计算时间差，相差的天数
            long betweenDay = DateUtil.between(time2,time, DateUnit.DAY);
            xrbRecent.setBetweenDay(betweenDay);
            xrbRecent.setBrName(carBrandsService.queryById(brId).getBrName());
            xrbRecent.setStName(carStyleService.queryById(stId).getStName());
            xrbRecent.setcPhoto(carDetail.getCPhoto());
            xrbRecent.setcMileage(carDetail.getCMileage());
            xrbRecent.setcDrive(carDetail.getCDrive());

            //上牌时间获取年的部分
            Date date3 = carDetail.getCRegistration();
            int y = DateUtil.year(date3);
            xrbRecent.setcRegistration(y);
            xrbRecent.setcPaifang(carDetail.getCPaifang());
            xrbRecentList.add(xrbRecent);
        }
        return xrbRecentList;
    }

    @Override
    public PageInfo<XrbRecent> dealCarPage(String brName, String stName, String year, String month, String pageNow, String pageSize) {
        PageHelper.startPage(Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        List<XrbRecent> xrbRecentList = queryDealCar(brName,stName,year,month);
        PageInfo<XrbRecent> xrbRecentPageInfo = new PageInfo<>(xrbRecentList);
        return xrbRecentPageInfo;
    }
}
